﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Threading;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;

namespace DotNet.Utilities
{
    public class HttpHelper
    {
        #region 变量定义
        private static CookieContainer cc = new CookieContainer();
        private static string contentType = "application/x-www-form-urlencoded";
        private static string accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1,text/html, application/xhtml+xml, */*";
        private static string userAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
        private static Encoding encoding = Encoding.GetEncoding("utf-8");
        private static int delay = 1000;
        private static int maxTry = 300;
        private static int currentTry = 0;
        #endregion

        #region 变量赋值
        /// <summary> 
        /// Cookie
        /// </summary> 
        public static CookieContainer CookieContainer
        {
            get
            {
                return cc;
            }
        }

        /// <summary> 
        /// 语言
        /// </summary> 
        /// <value></value> 
        public static Encoding Encoding
        {
            get
            {
                return encoding;
            }
            set
            {
                encoding = value;
            }
        }

        public static int NetworkDelay
        {
            get
            {
                Random r = new Random();
                return (r.Next(delay, delay * 2));
            }
            set
            {
                delay = value;
            }
        }

        public static int MaxTry
        {
            get
            {
                return maxTry;
            }
            set
            {
                maxTry = value;
            }
        }
        #endregion

        #region 获取HTML
        /// <summary>
        /// 获取HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">post 提交的字符串</param>
        /// <param name="isPost">是否是post</param>
        /// <param name="cookieContainer">CookieContainer</param>
        /// <returns>html </returns>
        public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer,WebProxy wp,Encoding ed)
        {
            if (string.IsNullOrEmpty(postData))
            {
                return GetHtml(url, cookieContainer,wp,ed,0);
            }


            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                byte[] byteRequest = null;
                if (ed != null)
                    byteRequest = ed.GetBytes(postData);
                else
                    byteRequest = encoding.GetBytes(postData);

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                //httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }
                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                currentTry = 0;

                httpWebRequest.Abort();
                httpWebResponse.Close();

                return html;
            }
            catch (Exception e)
            {
               

                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return string.Empty;
            }
        }

        public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer, WebProxy wp, Encoding ed,string refer)
        {
            if (string.IsNullOrEmpty(postData))
            {
                return GetHtml(url, cookieContainer, wp, ed,0);
            }


            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                byte[] byteRequest = null;
                if (ed != null)
                    byteRequest = ed.GetBytes(postData);
                else
                    byteRequest = encoding.GetBytes(postData);

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                //httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = refer;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }
                Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                currentTry = 0;

                httpWebRequest.Abort();
                httpWebResponse.Close();

                return html;
            }
            catch (Exception e)
            {


                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="cookieContainer">CookieContainer</param>
        /// <returns>HTML</returns>
        public static string GetHtml(string url, CookieContainer cookieContainer,WebProxy wp,Encoding ed,int count)
        {
           
            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
               // httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = "GET";
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, ed == null ? encoding : ed);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();

              

                httpWebRequest.Abort();
                httpWebResponse.Close();

                return html;
            }
            catch (Exception e)
            {

               
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }

                if (count < 4)
                {
                    System.Threading.Thread.Sleep(1000);
                    return GetHtml(url, cookieContainer, wp, ed, ++count);
                }
                return string.Empty;
            }
        }

        public static string GetHtml(string url, CookieContainer cookieContainer, WebProxy wp, Encoding ed,string refer,string contenttype,string useragent,string myaccept)
        {

            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contenttype == null ? contentType : contenttype;
                //httpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");  
                // httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = refer;
                httpWebRequest.Accept = myaccept == null ? accept : myaccept; ;
                httpWebRequest.UserAgent = useragent == null ? userAgent : useragent; 
                httpWebRequest.Method = "GET";
                //httpWebRequest.Headers.Add("Accept-Language", "zh-CN");
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, ed == null ? encoding : ed);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();



                httpWebRequest.Abort();
                httpWebResponse.Close();

                return html;
            }
            catch (Exception e)
            {


                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return string.Empty;
            }
        }

        public static string GetHtml(string url, CookieContainer cookieContainer, WebProxy wp, Encoding ed, string refer)
        {

            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType =  contentType ;

                // httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = refer;
                httpWebRequest.Accept =  accept  ;
                httpWebRequest.UserAgent =  userAgent ;
                httpWebRequest.Method = "GET";
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, ed == null ? encoding : ed);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();



                httpWebRequest.Abort();
                httpWebResponse.Close();

                return html;
            }
            catch (Exception e)
            {


                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return string.Empty;
            }
        }


        /// <summary>
        /// 获取HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <returns>HTML</returns>
        public static string GetHtml(string url,WebProxy wq,Encoding ed)
        {
            return GetHtml(url, cc, wq, ed,0);
        }
        /// <summary>
        /// 获取HTML
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="postData">提交的字符串</param>
        /// <param name="isPost">是否是POST</param>
        /// <returns>HTML</returns>
        public static string GetHtml(string url, string postData, bool isPost,WebProxy wp,Encoding ed)
        {
            return GetHtml(url, postData, isPost, cc, wp, ed);
        }
        /// <summary>
        /// 获取字符流
        /// </summary>
        /// <param name="url">地址</param>
        /// <param name="cookieContainer">cookieContainer</param>
        /// <returns>Stream</returns>
        public static Stream GetStream(string url, CookieContainer cookieContainer,WebProxy wp,Encoding ed)
        {
            //Thread.Sleep(delay); 

            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;

            try
            {

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                if (cookieContainer != null)
                {
                    httpWebRequest.CookieContainer = cookieContainer;
                }
                httpWebRequest.ContentType = contentType;
                //httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = "GET";
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                currentTry--;

                //httpWebRequest.Abort(); 
                //httpWebResponse.Close(); 

                return responseStream;
            }
            catch (Exception e)
            {
                

                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return null;
            }
        }

        public static Stream GetStream(string url, CookieContainer cookieContainer, WebProxy wp, Encoding ed,string refer)
        {
            //Thread.Sleep(delay); 

            HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;

            try
            {

                httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                if (cookieContainer != null)
                {
                    httpWebRequest.CookieContainer = cookieContainer;
                }
                httpWebRequest.ContentType = contentType;
                //httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = refer;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = "GET";
                if (wp != null)
                {
                    httpWebRequest.Proxy = wp;
                }

                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                currentTry--;

                //httpWebRequest.Abort(); 
                //httpWebResponse.Close(); 

                return responseStream;
            }
            catch (Exception e)
            {


                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return null;
            }
        }


        public static void Download(string url, CookieContainer cookieContainer, WebProxy wp, Encoding ed,string dir, string filename,ImageFormat format)
        {
            Stream data = GetStream(url, cookieContainer, wp, ed);
            System.Drawing.Image img = System.Drawing.Image.FromStream(data);
            img.Save(dir + filename, format);
 
        }


        public static IList<string> GetProxy(string url)
        {
            IList<string> list = new List<string>();
            WebRequest wr = WebRequest.Create(url);
            WebResponse myWebResponse = wr.GetResponse();
            Stream ReceiveStream = myWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.GetEncoding("gb2312");

            StreamReader readStream = new StreamReader(ReceiveStream, encode);

            string result = readStream.ReadToEnd();

            ReceiveStream.Close();
            myWebResponse.Close();

            Regex regex = new Regex(@"(\d+)\.(\d+)\.(\d+)\.(\d+):(\d+)", RegexOptions.IgnoreCase);
            MatchCollection mc = regex.Matches(result);
            if (mc.Count > 0)
            {
               
                foreach (Match m in mc)
                {
                    string v = m.Value;
                    list.Add(v);
                }
            }
            return list;
        }


        #endregion
    }
}
